package com.cfh.web.json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.cfh.ahead.Constant;
import com.cfh.domain.Config;
import com.cfh.domain.ConfigExample;
import com.cfh.domain.ProductExample;
import com.cfh.domain.ConfigExample.Criteria;
import com.cfh.service.ConfigService;
import com.cfh.service.ProductService;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ToolJson extends ActionSupport implements ModelDriven {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ToolJson.class);
	
	private static final long serialVersionUID = 1L;
	private ConfigService configService;
	private ProductService productService;
	private Map root = new HashMap();
	private Config Config = new Config();
	private String[] idsToDel = new String[200];
	public ToolJson() {
		super();

	}
	private int start;
	private int limit;
	private String sort;
	private String dir;
	
	private String SERACH_CODE;
	private String SERACH_TYPE;

	public String list() {
		ConfigExample example = new ConfigExample();
		Criteria crit = example.createCriteria();
		example.setOrderByClause(buildOrderString());
		if(Tools.hasText(SERACH_TYPE)){
			logger.debug("list()"); //$NON-NLS-1$
			crit.andCFG_TYPEEqualTo(SERACH_TYPE);
		}
		
		if(Tools.hasText(SERACH_CODE)){
			logger.debug("list()"); //$NON-NLS-1$
			crit.andCFG_CODELike("%"+SERACH_CODE+"%");
		}
		//add more search 
		if (start == 0 && limit == 0) {
			start = 0;
			limit = 999;
		}
		List list = configService.selectByExample(example, start, limit);
		//int count = configService.countByExample(example);
		
		int count = 0;
		try {
			count = configService.countByExample(example);
		} catch (Exception e) {
			count = Constant.MAX_COUNT_WHEN_EXCEPTION;
			logger.warn("list()", e); //$NON-NLS-1$
		}
		
		root.put("total", new Integer(count));
		root.put("items", list);
		return SUCCESS;
	}
	
	public String listFlv(){
		List list = configService.listFlv();
		root.put("items", list);
		return SUCCESS;
	}
	public String listPdf(){
		List list = configService.listPdf();
		root.put("items", list);
		return SUCCESS;
	}
	
	public String listProduct(){
		ProductExample example = new ProductExample();
		List list = productService.selectByExample(example , 0, 9999);
		root.put("items", list);
		return SUCCESS;
		
	}
	
	public String listLang(){
		List list = configService.listLangs();
		root.put("items", list);
		return SUCCESS;
		
	}
	
	public String listCompany(){
		List list = configService.listCompanys();
		root.put("items", list);
		return SUCCESS;
		
	}

	

	private String buildOrderString() {
		String toReturn = null;
		if (Tools.hasText(sort)) {
			toReturn = sort;
			if (Tools.hasText(dir)) {
				toReturn += " " + dir;
			}
		}
		return toReturn;
	}

	/***
	 * nouse
	 */
	public String execute() {

		return SUCCESS;
	}

	public Map getRoot() {
		return root;
	}

	
	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getLimit() {
		return limit;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getStart() {
		return start;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSort() {
		return sort;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getDir() {
		return dir;
	}

	public Object getModel() {

		return Config;
	}

	public void setIdsToDel(String[] idsToDel) {
		this.idsToDel = idsToDel;
	}

	public String[] getIdsToDel() {
		return idsToDel;
	}



	public void setConfigService(ConfigService ConfigService) {
		this.configService = ConfigService;
	}



	public ConfigService getConfigService() {
		return configService;
	}



	public void setSERACH_CODE(String sERACH_CODE) {
		SERACH_CODE = sERACH_CODE;
	}



	public String getSERACH_CODE() {
		return SERACH_CODE;
	}



	public void setSERACH_TYPE(String sERACH_TYPE) {
		SERACH_TYPE = sERACH_TYPE;
	}



	public String getSERACH_TYPE() {
		return SERACH_TYPE;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public ProductService getProductService() {
		return productService;
	}

}
